<?php

namespace Mifan\Alist\client;

use Closure;

class Options
{
    use Instances;

    /**
     * guzzle config
     * @var array
     */
    private $http_config = [];

    /**
     * @return array
     */
    public function getHttpConfig()
    {
        return array_merge([
            'base_uri' => $this->base_url . '/api/',
        ], $this->http_config);
    }

    /**
     * @param $http_config
     *
     * @return $this
     */
    public function setHttpConfig($http_config): self
    {
        $this->http_config = $http_config;

        return $this;
    }

    /**
     * @var string api
     */
    private $base_url;

    /**
     * @return string
     */
    public function getBaseUrl()
    {
        return $this->base_url;
    }

    /**
     * @param string $base_url
     *
     * @return $this
     */
    public function setBaseUrl($base_url): self
    {
        $this->base_url = $base_url;

        return $this;
    }

    /**
     * @see https://alist-public.apifox.cn/
     * @var array
     */
    public $apis = [
        'auth_login'        => ['method' => 'post', 'isAuth' => false, 'path' => 'auth/login'], // token获取
        'auth_register'     => ['method' => 'post', 'isAuth' => false, 'path' => 'auth/register'], // 用户注册
        'auth_login_hash'   => ['method' => 'post', 'isAuth' => false, 'path' => 'auth/login/hash'], // token获取hash
        'auth_2fa_generate' => ['method' => 'post', 'isAuth' => true, 'path' => 'auth/2fa/generate'], // 生成2FA密钥
        'auth_2fa_verify'   => ['method' => 'post', 'isAuth' => true, 'path' => 'auth/2fa/verify'], // 验证2FA code

        'me' => ['method' => 'get', 'isAuth' => false, 'path' => 'me'], // 获取当前用户信息

        'fs_list'                   => ['method' => 'post', 'isAuth' => false, 'path' => 'fs/list'], // 列出文件目录
        'fs_get'                    => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/get'], // 获取某个文件/目录信息
        'fs_dirs'                   => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/dirs'], // 获取目录
        'fs_search'                 => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/search'], // 搜索文件或文件夹
        'fs_mkdir'                  => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/mkdir'], // 新建文件夹
        'fs_rename'                 => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/rename'], // 重命名文件
        'fs_batch_rename'           => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/batch_rename'], // 批量重命名
        'fs_regex_rename'           => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/regex_rename'], // 正则重命名
        'fs_move'                   => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/move'], // 移动文件
        'fs_recursive_move'         => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/recursive_move'], // 聚合移动
        'fs_copy'                   => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/copy'], // 复制文件
        'fs_remove'                 => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/remove'], // 删除文件或文件夹
        'fs_remove_empty_directory' => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/remove_empty_directory'], // 删除空文件夹
        'fs_add_offline_download'   => ['method' => 'post', 'isAuth' => true, 'path' => 'fs/add_offline_download'], // 添加离线下载
        'fs_form'                   => ['method' => 'put', 'isAuth' => true, 'path' => 'fs/form'], // 表单上传文件
        'fs_put'                    => ['method' => 'put', 'isAuth' => true, 'path' => 'fs/put'], // 流式上传文件

        'public_settings' => ['method' => 'get', 'isAuth' => false, 'path' => 'public/settings'], // 获取站点设置
        '_ping'           => ['method' => 'get', 'isAuth' => false, 'path' => '/ping'], // ping检测

        'admin_meta_list'                            => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/meta/list'], // 列出元信息
        'admin_meta_get'                             => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/meta/get'], // 获取元信息
        'admin_meta_create'                          => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/meta/create'], // 新增元信息
        'admin_meta_update'                          => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/meta/update'], // 更新元信息
        'admin_meta_delete'                          => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/meta/delete'], // 删除元信息
        'admin_user_list'                            => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/user/list'], // 列出所有用户
        'admin_user_get'                             => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/user/get'], // 列出某个用户
        'admin_user_create'                          => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/user/create'], // 新建用户
        'admin_user_update'                          => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/user/update'], // 更新用户信息
        'admin_user_cancel_2fa'                      => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/user/cancel_2fa'], // 取消某个用户的两步验证
        'admin_user_delete'                          => ['method' => 'post', 'isAuth' => false, 'path' => 'admin/user/delete'], // 删除用户
        'admin_user_del_cache'                       => ['method' => 'post', 'isAuth' => false, 'path' => 'admin/user/del_cache'], // 删除用户缓存
        'admin_storage_create'                       => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/storage/create'], // 创建存储
        'admin_storage_update'                       => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/storage/update'], // 更新存储
        'admin_storage_list'                         => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/storage/list'], // 列出存储列表
        'admin_storage_enable'                       => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/storage/enable'], // 启用存储
        'admin_storage_disable'                      => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/storage/disable'], // 禁用存储
        'admin_storage_get'                          => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/storage/get'], // 查询指定存储信息
        'admin_storage_delete'                       => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/storage/delete'], // 删除指定存储
        'admin_storage_load_all'                     => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/storage/load_all'], // 重新加载所有存储
        'admin_driver_list'                          => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/driver/list'], // 查询所有驱动配置模板列表
        'admin_driver_names'                         => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/driver/names'], // 列出驱动名列表
        'admin_driver_info'                          => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/driver/info'], // 列出特定驱动信息
        'admin_setting_list'                         => ['method' => 'get', 'isAuth' => false, 'path' => 'admin/setting/list'], // 列出设置
        'admin_setting_get'                          => ['method' => 'get', 'isAuth' => false, 'path' => 'admin/setting/get'], // 获取某项设置
        'admin_setting_save'                         => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/setting/save'], // 保存设置
        'admin_setting_delete'                       => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/setting/delete'], // 删除设置
        'admin_setting_reset_token'                  => ['method' => 'post', 'isAuth' => false, 'path' => 'admin/setting/reset_token'], // 重置令牌
        'admin_setting_set_aria2'                    => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/setting/set_aria2'], // 设置aria2
        'admin_setting_set_qbit'                     => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/setting/set_qbit'], // 设置qBittorrent
        'admin_task_upload_info'                     => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/task/upload/info'], // 获取任务信息
        'admin_task_upload_done'                     => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/task/upload/done'], // 获取已完成任务
        'admin_task_upload_undone'                   => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/task/upload/undone'], // 获取未完成任务
        'admin_task_upload_delete'                   => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/task/upload/delete'], // 删除任务
        'admin_task_upload_cancel'                   => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/task/upload/cancel'], // 取消任务
        'admin_task_upload_retry'                    => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/task/upload/retry'], // 重试任务
        'admin_task_upload_clear_done'               => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/task/upload/clear_done'], // 清除已完成任务
        'admin_task_upload_clear_succeeded'          => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/task/upload/clear_succeeded'], // 清除已成功任务
        'admin_label_list'                           => ['method' => 'get', 'isAuth' => false, 'path' => 'admin/label/list'], // 标签列表
        'admin_label_create'                         => ['method' => 'post', 'isAuth' => false, 'path' => 'admin/label/create'], // 创建标签
        'admin_label_update'                         => ['method' => 'post', 'isAuth' => false, 'path' => 'admin/label/update'], // 修改标签
        'admin_label_get'                            => ['method' => 'get', 'isAuth' => false, 'path' => 'admin/label/get'], // 标签详情
        'admin_label_delete'                         => ['method' => 'post', 'isAuth' => false, 'path' => 'admin/label/delete'], // 删除标签
        'admin_label_file_binding_create'            => ['method' => 'post', 'isAuth' => false, 'path' => 'admin/label_file_binding/create'], // 文件绑定标签
        'admin_label_file_binding_get'               => ['method' => 'get', 'isAuth' => false, 'path' => 'admin/label_file_binding/get'], // 根据文件名称获取对应标签
        'admin_label_file_binding_delete'            => ['method' => 'post', 'isAuth' => false, 'path' => 'admin/label_file_binding/delete'], // 解除文件标签绑定关系
        'admin_label_file_binding_get_file_by_label' => ['method' => 'get', 'isAuth' => false, 'path' => 'admin/label_file_binding/get_file_by_label'], // 根据标签获取文件信息
        'admin_label_file_binding_create_batch'      => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/label_file_binding/create_batch'], // 批量设置文件标签
        'admin_role_list'                            => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/role/list'], // 获取角色列表
        'admin_role_get'                             => ['method' => 'get', 'isAuth' => true, 'path' => 'admin/role/get'], // 获取角色详情
        'admin_role_create'                          => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/role/create'], // 创建角色
        'admin_role_update'                          => ['method' => 'post', 'isAuth' => false, 'path' => 'admin/role/update'], // 更新角色
        'admin_role_delete'                          => ['method' => 'post', 'isAuth' => true, 'path' => 'admin/role/delete'], // 删除角色
    ];

    /**
     * @return array
     */
    public function getApis()
    {
        return $this->apis;
    }

    /**
     * @param array $apis
     *
     * @return $this
     */
    public function setApis(array $apis = []): self
    {
        $this->apis = array_merge($this->apis, $apis);
        return $this;
    }

    private $hooks = [
        'request_before' => [],
        'request_after'  => [],
    ];

    /**
     * 设置 hook 事件
     *
     * @param string  $event
     * @param Closure $action
     *
     * @return $this
     */
    public function setHooks(string $event, Closure $action): self
    {
        $this->hooks[$event][] = $action;
        return $this;
    }

    /**
     * @return array[]
     */
    public function getHooks(): array
    {
        return $this->hooks;
    }


}